package com.qfly.main.authentication.handler;

import com.qfly.main.common.bean.JwsToken;
import com.qfly.main.common.bean.Result;
import com.qfly.main.common.utils.JwtTokenUtil;
import com.qfly.main.common.utils.ResultUtil;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Wisdytech.cn
 * com.qfly.main.authentication.handler.qflyAuthenticationSuccessHandler
 *
 * @author hailin.zhl
 * @date 2018/12/04
 */
@Component
public class QflyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {

        User user = (User) authentication.getPrincipal();
        //创建token
        String jwtToken = JwtTokenUtil.generateToken(user.getUsername(), 3600, "_secret");
        JwsToken token = JwsToken.builder().token(jwtToken).build();
        Result<JwsToken> result = Result.success(token);
        ResultUtil.sendSuccess(request,response,result);

    }
}
